package controllers;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import play.mvc.Controller;

public class Application extends Controller {

	// public static void test() throws Exception {
	// Submission s1 = Submission.findById(1L);
	// Submission s2 = Submission.findById(1L);
	// Report report = new TextDiff().compare(s1.file.get(), s2.file.get());
	// render(report);
	// }

	public static String hash(String str) {
		String res = "";
		try {
			MessageDigest algorithm = MessageDigest.getInstance("MD5");
			algorithm.reset();
			algorithm.update(str.getBytes());
			byte[] md5 = algorithm.digest();
			String tmp = "";
			for (int i = 0; i < md5.length; i++) {
				tmp = (Integer.toHexString(0xFF & md5[i]));
				if (tmp.length() == 1) {
					res += "0" + tmp;
				} else {
					res += tmp;
				}
			}
		} catch (NoSuchAlgorithmException ex) {
		}
		return res;
	}

	public static String randomHash() {
		return randomHash(32);
	}

	public static String randomHash(int length) {
		return hash(System.currentTimeMillis() + "").substring(0, length);
	}

	public static void md5(String str) {
		renderText(hash(str));
	}
}